package com.sanjin.service.impl;

import com.sanjin.entity.ParkLot;
import com.sanjin.entity.ParkSpace;
import com.sanjin.exception.MyException;
import com.sanjin.repository.ParkSpaceRepository;
import com.sanjin.service.ParkLotService;
import com.sanjin.service.ParkSpaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: sanjin
 * @date: 2019/5/5 23:10
 */
@Service
public class ParkSpaceServiceImpl implements ParkSpaceService {

    @Autowired
    private ParkSpaceRepository parkSpaceRepository;

    @Autowired
    private ParkLotService parkLotService;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ParkSpace createParkSpace(ParkSpace parkSpace) {
        if (parkSpace.getId() != null)
            throw new MyException("create parkSpace,the id must be null");
        // 验证 parkLotId 是否存在
        ParkLot queryParkLot = parkLotService.queryParkLotById(parkSpace.getParkLotId());
        if (queryParkLot == null)
            throw new MyException("the parkLotId " + parkSpace.getParkLotId() + " is not exists.");
        // status 默认 0 -> 正常
        parkSpace.setStatus(0);
        parkSpaceRepository.save(parkSpace);
        return parkSpace;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteParkSpaceByParkSpaceId(Integer id) {
        parkSpaceRepository.deleteById(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteParkSpaceByParkSpaceIds(List<Integer> ids) {
        ids.forEach(id -> {
            parkSpaceRepository.deleteById(id);
        });
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ParkSpace queryParkSpaceByParkSpaceId(Integer id) {
        return parkSpaceRepository.findById(id).orElse(null);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ParkSpace updateParkSpace(ParkSpace parkSpace) {
        if (parkSpace.getId() == null)
            throw new MyException("update parkSpace,the id must not be null");
        parkSpaceRepository.save(parkSpace);
        return parkSpace;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Page<ParkSpace> queryParkSpaceByPage(PageRequest pageRequest) {
        Page<ParkSpace> page = parkSpaceRepository.findAll(pageRequest);
        return page;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public ParkSpace changeParkSpaceStatus(Integer parkSpaceId, Integer status) {
        ParkSpace parkSpace = this.queryParkSpaceByParkSpaceId(parkSpaceId);
        parkSpace.setStatus(status);
        parkSpaceRepository.save(parkSpace);
        return parkSpace;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ParkSpace> queryParkSpacesByParkLotId(Integer parkLotId) {
        return parkSpaceRepository.findParkSpacesByParkLotId(parkLotId);
    }

    @Transactional
    @Override
    public void save(ParkSpace parkSpace) {
        parkSpaceRepository.save(parkSpace);
    }

    @Override
    public ParkSpace queryParkSpaceByLoraAddr(String loraAddr) {
        return parkSpaceRepository.findParkSpacesByLoraAddr(loraAddr);
    }
}
